# 用pandas框架读取数据并使用matplotlib绘图
import pandas as pd
import matplotlib.pyplot as plt
import scipy.interpolate as itp

# 处理超过阈值的数据
def defectsCop(data_series, threshold):
    for index in range(0, len(data_series)):
        item = data_series[index]
        if item >= float(threshold):
            item = None
            data_series[index] = item

# 如果数据是None，利用此数据前后的平均值进行插值
def seriesItp(data_series):
    for index in range(0, len(data_series)):
        item = data_series[index]
        if pd.isnull(data_series[index]):
            x_list = [index-1, index+1]
            y_list = [data_series[index-1], data_series[index+1]]
            lagange_poly = itp.lagrange(x_list, y_list)
            data_series[index] = lagange_poly(index)

ug_data = pd.read_csv(
    'ug_detect.csv',
    header=0,
    encoding='gb2312'
)
temperature_data = ug_data[u'温度（?C）']
humidity_data = ug_data[u'相对湿度']
gas_data = ug_data[u'瓦斯(m?/min)']
co_data = ug_data[u'一氧化碳(m?/min)']

defectsCop(temperature_data, 60)
defectsCop(humidity_data, 200)
defectsCop(gas_data, 100)
defectsCop(co_data, 100)

seriesItp(temperature_data)
seriesItp(humidity_data)
seriesItp(gas_data)
seriesItp(co_data)

ug_data = pd.read_csv(
    'ug_detect.csv',
    header=0,
    encoding='gb2312'
)
# print("原始数据：\n", ug_data[u'温度（?C）'])
# print("清洗后的数据：\n", temperature_data)

temp_data_org = ug_data[u'温度（?C）']
gas_data_org = ug_data[u'瓦斯(m?/min)']
# 绘制折线图
# t = range(len(temp_data_org))
# plt.plot(t, temp_data_org)
# plt.plot(t, temperature_data)
# plt.plot(t, temperature_data, 'pr')
# plt.show()

# 作业：瓦斯浓度绘制折线图
t = range(len(gas_data))
plt.plot(t, gas_data)
plt.plot(t, gas_data, 'pr')
plt.show()